package com.hxc.common.config.interceptors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoggerInterceptor implements HandlerInterceptor {
    public static final Logger logger = LoggerFactory.getLogger("IPLog");

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) {

//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("application/json; charset=utf-8");
//
//        // 如果不是映射到方法直接通过
//        if (!(object instanceof HandlerMethod)) {
//            return true;
//        }
//        HandlerMethod handlerMethod = (HandlerMethod) object;
//        Method method = handlerMethod.getMethod();
//        //检查是否有MyLogger注释，有则记录ip
//        if (method.isAnnotationPresent(MyLogger.class)) {
//            MyLogger myLogger = method.getAnnotation(MyLogger.class);
//            if (myLogger.required()) {
//                ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
//                logger.info( "[ " + IPUtils.getIpAddr(request) + " ] " + method.getName() + "---" + apiOperation.value());
//            }
//        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response,
                           Object object, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response,
                                Object object, Exception e) throws Exception {

    }
}
